package com.aioz.security.service.impl;

import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.Category;
import com.aioz.security.entity.CategoryMenu;
import com.aioz.security.mapper.CategoryMapper;
import com.aioz.security.mapper.CategoryMenuMapper;
import com.aioz.security.service.CategoryMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aioz
 * @since 2022-10-18
 */
@Service
public class CategoryMenuServiceImpl extends ServiceImpl<CategoryMenuMapper, CategoryMenu> implements CategoryMenuService {

    @Resource
    private CategoryMenuMapper categoryMenuMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public ResponseResult topCategoryMenus() {
        List<CategoryMenu> categoryMenus = categoryMenuMapper.selectList(null);
        List<String> topMenus = categoryMenus.stream()
                            .map(CategoryMenu::getCategoryMenu)
                            .limit(9)
                            .collect(Collectors.toList());
        return new ResponseResult(200,topMenus);
    }

    @Override
    public ResponseResult getCategoryMenus() {
        List<CategoryMenu> categoryMenus = categoryMenuMapper.selectList(null);
        categoryMenus = categoryMenus.stream().limit(6).collect(Collectors.toList());
        for (CategoryMenu categoryMenu : categoryMenus) {
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.eq("cm_id",categoryMenu.getId());
            List<Category> categories =  categoryMapper.selectList(wrapper);
            categories = categories.stream().limit(3).collect(Collectors.toList());
            categoryMenu.setCategories(categories);
        }
        return new ResponseResult(200,categoryMenus);
    }
}
